﻿using Comm;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBHelper
{
    public class DbContext
    {
        private static string sqlcon = "server=127.0.0.1;database=Product;uid=sa;pwd=123456";

        public static bool AddModel<T>(T model) where T : class, new()
        {
            SqlHelper sql = new SqlHelper(sqlcon);
            string sqlstr = sql.GetInsertStr<T>(model);
            return sql.SqlExecuteNon(sqlstr) > 0;

        }

        public static bool DeleteModel<T>(string con) where T : class, new()
        {
            int count = -1;
            SqlHelper sql = new SqlHelper(sqlcon);
            string sqlstr = sql.GetDeleteStr<T>(con);
            count = sql.SqlExecuteNon(sqlstr);
            return count>0;
        }
        public static bool UpdateModel<T>(T mold, T mnew) where T : class, new()
        {
            int count = -1;
            SqlHelper sql = new SqlHelper(sqlcon);
            string sqlstr = sql.GetUpdateStr<T>(mold, mnew);
            count = sql.SqlExecuteNon(sqlstr);
            return count>0;
        }
        /// <summary>
        /// 获取单个查询结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlconstr"></param>
        /// <param name="con"></param>
        /// <returns></returns>
        public static T GetModel<T>(string con) where T : class, new()
        {
            return GetModels<T>(con)?.FirstOrDefault();
        }
        /// <summary>
        /// 获取结果查询集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sqlconstr">数据库连接字符串</param>
        /// <param name="con">条件</param>
        /// <returns></returns>
        public static List<T> GetModels<T>( string con) where T : class, new()
        {
            SqlHelper sql = new SqlHelper(sqlcon);
            string sqlstr = sql.GetSelectStr<T>(con);
            DataTable dt = sql.SqlExecuteReader(sqlstr);
            return dt.ToList<T>();
        }
    }
    public static class DbHelper
    {
        public static List<T> ToList<T>(this DataTable dt) where T : class, new()
        {
            Type ty = typeof(T);
            if (dt.Rows.Count < 1)
            {
                return null;
            }
            var pros = ty.GetProperties();
            if (pros == null)
            {
                return null;
            }
            List<T> lst = new List<T>();
            foreach (DataRow row in dt.Rows)
            {
                var t = new T();
                foreach (var pro in pros)
                {
                    if (row[pro.Name] is DBNull)
                    {

                    }
                    else
                    {
                        try
                        {
                            pro.SetValue(t, row[pro.Name]);
                        }
                        catch (Exception ex)
                        {
                            Logger.WriteSysLog(ex.Message, ex);
                        }

                    }
                }
                lst.Add(t);
            }
            return lst;
        }
    }
}
